package com.zx.baofu.alcohol.util;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MapUtil {
    private static final String SWITCH_POSITION_URL = "http://api.map.baidu.com/geoconv/v1/";
    private static final String POSITION_TO_ADDRESS_URL = "http://api.map.baidu.com/reverse_geocoding/v3/";
    static final String AK = "GE96M0cZiWZAXtrKdh81MRAdruzLUQfu";
    /**
     * 请求超时时间。单位毫秒
     */
    private static final int outTime = 1000*5;
    /**
     * 用过百度坐标解析地址
     * @param longitude
     * @param latitude
     * @return {"result":{"formatted_address":"河北省保定市竞秀区五四中路1350号","business":"五四路,瑞祥大街,恒祥北大街","sematic_description":"","cityCode":307,"roads":[],"location":{"lng":115.49467048259763,"lat":38.881806258166019},"poiRegions":[],"pois":[],"addressComponent":{"city_level":2,"country":"中国","town":"","distance":"55","city":"保定市","adcode":"130602","country_code_iso":"CHN","country_code_iso2":"CN","country_code":0,"town_code":"","province":"河北省","street":"五四中路","district":"竞秀区","street_number":"1350号","direction":"南"}},"status":0}
     */
    public static JSONObject getCityFromLngAndlat(double longitude, double latitude){
        String param = "ak="+ AK +"&output=json&coordtype=bd09ll&location="+latitude+","+longitude;
        String responseStr = HttpUtil.sendGet(POSITION_TO_ADDRESS_URL, param);
        JSONObject response = JSONObject.parseObject(responseStr);
        return response;
    }

    /**
     * GPS转百度
     * @param longitude
     * @param latitude
     * @return  {"result":[{"x":115.4946704825977,"y":38.881806155372199}],"status":0}
     *  返回结果中包含经度的key为 x ，纬度的key为 y
     */
    public static JSONObject GPSToBaiDu(double longitude, double latitude){
        String param = "coords="+ longitude + "," + latitude + "&from=1&to=5&ak=" + AK;
        String responseStr = HttpUtil.sendGet(SWITCH_POSITION_URL, param);
        JSONObject response = JSONObject.parseObject(responseStr);
        return response;
    }

    /*public static void main(String[] args) {
        //115.451904, 38.916206
        JSONObject res1 = GPSToBaiDu(116.046112,38.977823);
        System.out.println(res1);
        double baiduLongitude = JSONObject.parseObject(res1.getJSONArray("result").get(0).toString()).getDouble("x");
        double baiduLatitude = JSONObject.parseObject(res1.getJSONArray("result").get(0).toString()).getDouble("y");
        JSONObject res2 = getCityFromLngAndlat(baiduLongitude,baiduLatitude);
        System.out.println(JSONObject.toJSONString(res2));
    }*/

    /**
     * 计算地球上任意两点(经纬度)距离
     *
     * @param long1
     *            第一点经度
     * @param lat1
     *            第一点纬度
     * @param long2
     *            第二点经度
     * @param lat2
     *            第二点纬度
     * @return 返回距离 单位：米
     */
    public static double distance(double long1, double lat1, double long2,  double lat2) {
        double a, b, R;
        R = 6378137; // 地球半径
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2  * R  * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
        return d;
    }

    public static void main(String[] args) {
       double meters = distance(115.994513,38.942381,116.279096,39.12847);
       System.out.println(meters);
    }
}
